# @File    : logUtil.py
# @Project : JBL_Android&iOS
import logging
import os
from datetime import datetime
import colorlog
from logging.handlers import TimedRotatingFileHandler

log_colors_config = {
    "DEBUG": "blue",
    "INFO": "green",
    "WARNING": "red",
    "ERROR": "yellow",
    "CRITICAL": "red,bold",
}


class Logging:

    log_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + "\\" + 'logs'
    android_name = 'android' + ''.join('.log')
    ios_name = 'ios' + ''.join('.log')

    def create_log_android(self, logger_name=android_name):
        self.logger = logging.getLogger(logger_name)
        self.logger.setLevel(logging.INFO)

        if not self.logger.handlers:
            """1、创建文件日志路径"""
            self.file_log_path = self.log_path + "\\" + self.android_name
            self.file_handler = TimedRotatingFileHandler(filename=self.file_log_path, when="D", interval=1,
                                                         backupCount=1, delay=True, encoding='utf-8')
            current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            self.file_handler.formatter = logging.Formatter(
                f'[{current_time}] - [%(levelname)s] - [%(lineno)d]: %(message)s')
            self.logger.addHandler(self.file_handler)

        return self.logger

    def create_log_ios(self, logger_name=ios_name):

        self.logger = logging.getLogger(logger_name)
        self.logger.setLevel(logging.INFO)

        if not self.logger.handlers:
            """1、创建文件日志路径"""
            self.file_log_path = self.log_path + "\\" + self.ios_name
            self.file_handler = TimedRotatingFileHandler(filename=self.file_log_path, when="D", interval=1,
                                                         backupCount=1, delay=True, encoding='utf-8')
            current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            self.file_handler.formatter = logging.Formatter(
                f'[{current_time}] - [%(levelname)s] - [%(lineno)d]: %(message)s')
            self.logger.addHandler(self.file_handler)

        return self.logger


def info_a(msg):
    Logging().create_log_android().info(msg)


def info_i(msg):
    Logging().create_log_ios().info(msg)


def debug_a(msg):
    Logging().create_log_android().debug(msg)


def warning_a(msg):
    Logging().create_log_android().warning(msg)


def warning_i(msg):
    Logging().create_log_ios().warning(msg)
